import {dateFormat} from '../../utils/util.js'

Component({
    data: {
        weekCalendar: [],
        monthCalendar: [],
        todayMonth: '',
        today: 0,
        select: 0,
        todoList: {},
        isWeek: true,
        current: 0,
        oldDate: undefined,
    },
    properties: {
        todo: Object,
        date: String
    },
    observers: {
        'todo': function (todo) {
            this.setData({
                todoList: todo
            })
        },
        'date': function (date) {
            if(date){
                this.selectTime(date)
            }
            else{
                this.selectTime(dateFormat('YYYY-mm-dd',new Date()))
            }
            this.getWeekArray()
        },
    },
    ready(){
        this.onMonthSwitch({
            detail:{
                current: this.data.current
            }
        })
    },
    methods: {
        getWeekArray(date = new Date().getTime()) {
            this.setData({
                weekCalendar: this.getDayArray(new Date(date)),
                current: 1
            })
        },
        getMonthArray(date = new Date().getTime()) {
            const calendar = []
            const weekNum = 24
            // 获取最近一年的日历
            const all = this.getDayArray(new Date(date), weekNum * 4)
            console.log(all)
            for (let i = 0; i < all.length; i += 4) {
                calendar.push(all.slice(i, i + 4))
            }
            console.log((Math.ceil(weekNum / 2) - 1))
            this.setData({
                'monthCalendar': calendar,
                current: Math.floor(weekNum/2) - 1
            })
        },
        getToday() {
            let date = new Date(new Date().setHours(0, 0, 0, 0))
            // 设置今日时间
            this.setData({
                today: date.getTime()
            })
            // 默认选中当前日期
            this.selectTime(dateFormat('YYYY-mm-dd', date))
        },
        // selectDay  选中日期   生成周列表
        getDayArray(selectDay, weekNum = 4) {
            let date = new Date(selectDay.setHours(0, 0, 0, 0))
            // 每天的毫秒数
            const dayTime = 1000 * 60 * 60 * 24

            // 当前周几
            const todayWeek = date.getDay()

            // 将时间重置为周一
            date = new Date(date.getTime() - ((todayWeek - 1) * dayTime))

            // 循环以得到三周的列表
            const calendars = []
            let num = -7 * (Math.ceil(weekNum / 2) - 1) - 1
            console.log(num)
            for (let i = 0; i < weekNum; i++) {
                const calendar = []
                for (let j = 0; j < 7; j++) {
                    const date2 = new Date(date.getTime() + (num * dayTime))
                    calendar.push({
                        day: date2.getDate(),
                        date: this.getYearMonth(date2),
                        time: date2.getTime(),
                        fulldate: dateFormat('YYYY-mm-dd', date2)
                    })
                    num += 1
                }
                calendars.push(calendar)
            }
            console.log(calendars)
            return calendars
        },

        getYearMonth(time) {
            return time.getFullYear() + '-' + (time.getMonth() < 9 ? ('0' + (time.getMonth() + 1)) : (time.getMonth() + 1))
        },
        checkType() {
            this.setData({
                isWeek: !this.data.isWeek
            })
            this.upCalendarSelectDay()
        },
        upCalendarSelectDay(){
            if(this.data.isWeek){
                this.getWeekArray(new Date(this.data.select))
            }
            else{
                this.getMonthArray(new Date(this.data.select))
            }
        },
        userSelect(event){
            this.selectTime(event.detail.value)
            this.upCalendarSelectDay()
            this.triggerEvent('monthChange', event.detail.value)
        },
        selectTime(event) {
            const time = event.currentTarget ? event.currentTarget.dataset.fulldate : event
            if(time === this.data.oldDate){
                return 
            }
            this.setData({
                select: time,
                todayMonth: dateFormat('YYYY-mm-dd', new Date(time))
            })
            this.data.oldDate = time
            this.triggerEvent('dateChange', time)
        },
        // 日期切换，通知父组件更新todo
        onMonthSwitch(event){
            let date = ''
            if(this.data.isWeek){
                date = this.data.weekCalendar[event.detail.current][0].fulldate
            }
            else{
                date = this.data.monthCalendar[event.detail.current][0][0].fulldate
            }
            this.triggerEvent('monthChange', {
                type: this.data.isWeek ? 'week' : 'month',
                date
            })
        }
    }
})